package com.gz.DroidPong.Constants;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.util.Log;

public class TextureManager {

	public static final int WHITE_BALL_TEXTURE = 0;
	public static final int CUP_BLACK_TEXTURE = WHITE_BALL_TEXTURE + 1;
	public static final int CUP_RED_TEXTURE = CUP_BLACK_TEXTURE + 1;
	public static final int CUP_GREEN_TEXTURE = CUP_RED_TEXTURE + 1;
	public static final int CUP_PURPLE_TEXTURE = CUP_GREEN_TEXTURE + 1;
	public static final int CUP_FRONT_BLACK_TEXTURE =CUP_PURPLE_TEXTURE + 1; 
	
	private static TextureRegion mWhiteBallTR;
	private static TextureRegion mCupBlackTR;
	private static TextureRegion mCupFrontBlackTR;
	
	public TextureManager(Engine pEngine){	
		log("Init textures");
		setState("Initializing textures");
		final Texture texture = new Texture(1024,1024,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		setState("Loading White Ball");
		mWhiteBallTR = TextureRegionFactory.createFromAsset	(texture, UIManager.GetGame(), "balls/grey.png", 0,0);//256,256
		setState("Loading Black Cup");
		mCupBlackTR = TextureRegionFactory.createFromAsset	(texture, UIManager.GetGame(), "cups/blk.png",257,0);//171*256 = 427,256
		mCupFrontBlackTR = TextureRegionFactory.createFromAsset(texture, UIManager.GetGame(), "cups/blk_front.png", 514, 0);
		
		
		setState("Loading Textures");
		pEngine.getTextureManager().loadTexture(texture);
		//pEngine.getFontManager().loadFont(mFont);
		log("Textures Loaded");
	}
	
	public TextureRegion getTexture(int pTextureID){
		switch(pTextureID){
		case WHITE_BALL_TEXTURE:
			return mWhiteBallTR.clone();
		case CUP_BLACK_TEXTURE:
			return mCupBlackTR.clone();
		case CUP_FRONT_BLACK_TEXTURE:
			return mCupFrontBlackTR.clone();
		default:
				return null;
		}
	}
	
	private void setState(String msg){
		log("UILog: " + msg);
		UIManager.setLoadState(msg);
	}
	
	public float getHeightFromWidthByID(int pTextureID, float pWidth){
		return getHeightFromWidth(getTexture(pTextureID),pWidth);
		
	}
	
	public static float getHeightFromWidth(TextureRegion tr, float pWidth){
		float x = pWidth / tr.getWidth();
		return tr.getHeight() * x;
	}
	
	public static float getHeightFromWidthTiled(TiledTextureRegion tr, float pWidth){
		if(tr == null)return -1;
		float x = pWidth / tr.getTileWidth();
		
		return tr.getTileHeight() * x;
	}
	
	private void log(String msg){
		Log.d("TextureManager",msg);
	}
}
